<?php

class Dao_CategoryDAO extends Dao_AbstractDAO {
	
	public function __construct($db = null) {
		parent::__construct($db);
		$this->tableName = "M_CATEGORY";
		$this->key = "ID";
		$this->classModel = "Model_Category";
	}
	
	public function getAllCategory($number = null) {
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " WHERE T.ENABLE = '1' ";
		$query .= " AND T.DELETE_TM IS NULL ";
		$query .= " ORDER BY T.CREATE_TM DESC ";
		
		if ($number !== null) {
			$query .= " limit 0, $number ";
		}
		
		$db = $this->db;
		$res = $db->query($query);
		
		return $this->getObjects($res);
	}
	
	/**
	 * 
	 * @param unknown $id
	 * @return Model_Category
	 */
	public function getCategoryById ($id) {
		$query = " 
			SELECT
				CA.ID,
				CA.`NAME`,
				CA.VIEW_TIMES,
				CA.IMAGE_FILE,
				CA.`ENABLE`,
				CA.ORDER_INDEX,
				CA.CREATE_TM,
				CA.CREATE_USR,
				CA.UPDATE_TM,
				CA.UPDATE_USR,
				CA.DELETE_TM
			FROM
				M_CATEGORY CA
			WHERE
				CA.ID = '".$id."'
				AND
				CA.DELETE_TM IS NULL
				AND
				CA.`ENABLE` = '1'
		";
		
	
		$db = $this->db;
		$res = $db->query($query);
	
		$listItem =  $this->getObjects($res);
		
		if (count($listItem)>0){
			return $listItem[0];
		}else{
			return null;
		}
	}
}